/*
 *  Copyright (c) 2011 Mathew Hall.
 *  All rights reserved.
 * 
 *  Redistribution and use in source and binary forms, with or
 *  without modification, are permitted provided that the following conditions
 *  are met:
 * 
 *  Redistributions of source code must retain the above copyright
 *  notice, this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above
 *  copyright notice, this list of conditions and the following
 *  disclaimer in the documentation and/or other materials provided
 *  with the distribution.
 *  Neither the name of the University of Sheffield nor the names of its
 *  contributors may be used to endorse or promote products derived
 *  from this software without specific prior written permission.
 * 
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
 *  CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 *  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 *  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 *  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package search.fitnessfunctions;

import primitives.cluster.*;
import primitives.graph.Node;
import static Math.log;
/**
 *
 * @author Mathew Hall
 */
class WorkingMemoryLimitFF extends TreeFitnessFunction{
    def connected = {it.getTransitionsAsN().size() > 0}
    def unconnected = {!connected(it)}


    double numChildren(IClusterLevel c){
	if(!c.encapsulates()){
		return c.nodes.size();
	}else{
		return c.children.size();
	}
	
    }

   double fx(double x){
	double boost = 0;
	if(x >= 9){
		boost =0.13
	}
//	=(1/ABS(-$C$2*((A2 -7)^2) -LN(A2+0.00000000001))) + IF(A2 <=$C$3, $C$4, 0)
	
	double logpart = Math.log(x + 0.0000000000001)
	double smoothing = -0.3*(Math.pow(x - 7,2))
	double outn =  (1.0 / Math.abs(smoothing - logpart ) )+ boost
	if(outn <= 0){
		System.err.println("Going to return $smoothing - $logpart + $boost which gives you $outn")
		
	}
	return outn
   }

	
    double evaluate(ClusterHead tree) {
        double total = 0;
		
        def modules = [];
         
        
        tree.getChildren().each{modules.add(it)}
        
        IClusterLevel current;
        def curr = 0;
        while (curr < modules.size()){
            current = modules.get(curr)
		if(current.encapsulates())
            		modules.addAll(current.children)
            curr++
	    total += numChildren(current);
	    
        }
	double fitness = fx(total/modules.size())
	
	if(fitness < 0){
		System.err.println("ERROR: FITNESS IS < 0 " + fitness)
	}else if(fitness == 0.0){
		System.err.println("ERROR: FITNESS IS 0 for " + modules.size() + " modules, total count: " + total + " for fx of  " + (total/modules.size()))
	}
	
	return fitness;

        

        
        
    }
    
 
}

